package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.acct.facade.request.VirtAcctTransStatusQueryRequest;
import cc.rengu.igas.acct.facade.response.VirtAcctTransStatusQueryResponse;
import cc.rengu.igas.acct.facade.result.Result;
import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.dao.ManualAcctInfoMapper;
import cc.rengu.igas.bomp.common.dao.ManualErrAdjustTxnMapper;
import cc.rengu.igas.bomp.common.dao.ManualReconErrTxnMapper;
import cc.rengu.igas.bomp.common.dao.impl.ManualAcctInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.ManualErrAdjustTxnMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.ManualReconErrTxnMapperImpl;
import cc.rengu.igas.bomp.common.entity.ErrAdjustTxn;
import cc.rengu.igas.bomp.common.entity.ManualAcctInfo;
import cc.rengu.igas.bomp.common.entity.ReconErrTxn;
import cc.rengu.igas.bomp.common.enums.AcctStatusEnum;
import cc.rengu.igas.bomp.common.enums.AdjustStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.enums.TppsErrTxnProdTypeEnum;
import cc.rengu.igas.bomp.core.service.base.PayService;
import cc.rengu.igas.bomp.facade.request.ManualAccoutingStatusQueryRequest;
import cc.rengu.igas.bomp.facade.response.ManualAccoutingStatusQueryResponse;
import cc.rengu.igas.channel.upac.facade.request.TokenTransStateQueryRequest;
import cc.rengu.igas.channel.upac.facade.response.TokenTransStateQueryRespone;
import cc.rengu.igas.momp.facade.bean.TxnRegulationStatusBean;
import cc.rengu.igas.momp.facade.request.QueryTxnRegulationResultRequest;
import cc.rengu.igas.momp.facade.response.QueryTxnRegulationResultResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.MchntOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.PayOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.MchntOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.PayOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.ChannelCallCfg;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.util.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 手工调账或挂账状态查询
 * Created by moxm on 2020/6/30
 */
public class ManualAccoutingStatusQueryService extends PayService {
    /*手工调账*/
    private final String adjustmentFla = "00";

    /*手工挂账*/
    private final String paymentFla = "01";

    @Override
    protected Object bizBeforeCust() throws Exception {
        ManualAccoutingStatusQueryRequest manualAccoutingStatusQueryRequest = new ManualAccoutingStatusQueryRequest();
        ConvertUtil.convertOutput(manualAccoutingStatusQueryRequest);
        return manualAccoutingStatusQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ManualAccoutingStatusQueryRequest manualAccoutingStatusQueryRequest = (ManualAccoutingStatusQueryRequest) request;
        ManualAccoutingStatusQueryResponse manualAccoutingStatusQueryResponse = new ManualAccoutingStatusQueryResponse();
        BeanUtil.beanCopy(manualAccoutingStatusQueryRequest, manualAccoutingStatusQueryResponse);

        /*机构标识*/
        String instId = manualAccoutingStatusQueryRequest.getHeader().getInstId();
        /*挂账/调账唯一标识*/
        String acctUnionKey = manualAccoutingStatusQueryRequest.getAcctUnionKey();
        /*记账类型*/
        String acctOprType = manualAccoutingStatusQueryRequest.getAcctOprType();

        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(acctUnionKey) || StringUtil.isEmptyOrNull(acctOprType)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (adjustmentFla.equals(acctOprType)) {
            /*手工调账*/
            /*先查调账流水表状态*/
            ManualErrAdjustTxnMapper manualErrAdjustTxnMapper = new ManualErrAdjustTxnMapperImpl();
            ErrAdjustTxn errAdjustTxn = manualErrAdjustTxnMapper.selectManualErrAdjustTxnByprimaryKeyNew(acctUnionKey);
            if (null == errAdjustTxn) {
                throw new BizException(RespCodeEnum.RECORD_NOT_FOUND.getRespCode(), RespCodeEnum.RECORD_NOT_FOUND.getRespDesc());
            }
            /*记账状态*/
            String acctStatus = errAdjustTxn.getAcctStatus();
            if (AcctStatusEnum.SUCCESS.getAcctStatus().equals(acctStatus) || AcctStatusEnum.FAIL.getAcctStatus().equals(acctStatus)) {
                /*如果为成功或失败，直接返回响应信息*/
                String acctDate = errAdjustTxn.getAcctDate();
                String acctSeqNum = errAdjustTxn.getAcctSeqNum();
                manualAccoutingStatusQueryResponse.setAcctStatus(acctStatus);
                manualAccoutingStatusQueryResponse.setAcctDate(acctDate);
                manualAccoutingStatusQueryResponse.setAcctSeqNum(acctSeqNum);
                manualAccoutingStatusQueryResponse.setFailureReason(errAdjustTxn.getRespDesc());
            } else {
                /*其他状态，调通道查询调账状态*/
                ManualReconErrTxnMapper manualReconErrTxnMapper = new ManualReconErrTxnMapperImpl();
                ReconErrTxn reconErrTxn = manualReconErrTxnMapper.selectReconErrTxnByprimaryKey(acctUnionKey);
                /*是否有数据校验*/
                if (null == reconErrTxn) {
                    rglog.error("获取对账不平信息不存在，instId:<{}>,acctUnionKey:<{}>", instId, acctUnionKey);
                    throw new BizException(RespCodeEnum.RECORD_NOT_FOUND.getRespCode(), RespCodeEnum.RECORD_NOT_FOUND.getRespDesc());
                }
                /*对账渠道*/
                String prodType = reconErrTxn.getBizProdType();
                if (TppsErrTxnProdTypeEnum.VIRTUAL_ACCOUNT.getProdType().equals(prodType)) {
                    /*虚拟账户调账状态查询*/
                    Optional<ChannelCallCfg> channelCallCfgOptional = txnInfo.getChannelCallCfgList().stream().filter(item -> ChannelEnum.ACCT.getChannelType().equals(item.getCallChannelId())).findFirst();
                    if (!channelCallCfgOptional.isPresent()) {
                        rglog.error("通道服务调用配置表无数据！,instId:<{}>,traceNo:<{}>,",
                                instId, manualAccoutingStatusQueryRequest.getHeader().getTraceNo());
                        throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
                    }
                    //获取通道服务调用配置
                    ChannelCallCfg channelCallCfg = channelCallCfgOptional.get();
                    String acctSeqNum = errAdjustTxn.getAcctSeqNum();
                    manualAccoutingStatusQueryResponse = virtualAccountStatusQuery(instId, reconErrTxn, acctSeqNum, channelCallCfg);
                } else if (TppsErrTxnProdTypeEnum.MARKETING.getProdType().equals(prodType)) {
                    /*营销对账状态查询*/
                    Optional<ChannelCallCfg> channelCallCfgOptional = txnInfo.getChannelCallCfgList().stream().filter(item -> ChannelEnum.MOMP.getChannelType().equals(item.getCallChannelId())).findFirst();
                    if (!channelCallCfgOptional.isPresent()) {
                        rglog.error("通道服务调用配置表无数据！,instId:<{}>,traceNo:<{}>,",
                                instId, manualAccoutingStatusQueryRequest.getHeader().getTraceNo());
                        throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
                    }
                    //获取通道服务调用配置
                    ChannelCallCfg channelCallCfg = channelCallCfgOptional.get();
                    manualAccoutingStatusQueryResponse = marketingStatusQuery(instId, errAdjustTxn, channelCallCfg);
                } else {
                    rglog.error("获取对账类型，不允许发起调账状态查询请求，prodType:<{}>", instId, prodType);
                    throw new BizException(RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespCode(), RespCodeEnum.NOT_SUPPORT_ORT_TYPE.getRespDesc());
                }
            }
        } else if (paymentFla.equals(acctOprType)) {
            /*手工挂账*/
            /* 根据挂账唯一标识校验挂账状态 */
            ManualAcctInfoMapper manualAcctInfoMapper = new ManualAcctInfoMapperImpl();
            ManualAcctInfo manualAcctInfo = manualAcctInfoMapper.selectManualAcctInfoById(instId, acctUnionKey);
            if (null == manualAcctInfo) {
                rglog.error("挂账记录不存在，不允许发起挂账状态查询请求，instId:<{}>,acctUnionKey:<{}>", instId, acctUnionKey);
                throw new BizException(RespCodeEnum.RECORD_NOT_FOUND.getRespCode(), RespCodeEnum.RECORD_NOT_FOUND.getRespDesc());
            }
            /*挂账状态*/
            String acctStatus = manualAcctInfo.getAcctStatus();
            if (AcctStatusEnum.SUCCESS.getAcctStatus().equals(acctStatus) || AcctStatusEnum.FAIL.getAcctStatus().equals(acctStatus)) {
                /*如果为成功或失败，直接返回响应信息*/
                String acctDate = manualAcctInfo.getAcctDate();
                String acctSeqNum = manualAcctInfo.getAcctSeqNum();
                manualAccoutingStatusQueryResponse.setAcctStatus(acctStatus);
                manualAccoutingStatusQueryResponse.setAcctDate(acctDate);
                manualAccoutingStatusQueryResponse.setAcctSeqNum(acctSeqNum);
                manualAccoutingStatusQueryResponse.setFailureReason(manualAcctInfo.getAdjustReason());
            } else {
                /*其他状态，调通道查询挂账状态*/
                String acctSeqNum = manualAcctInfo.getAcctSeqNum();
                Optional<ChannelCallCfg> channelCallCfgOptional = txnInfo.getChannelCallCfgList().stream().filter(item -> ChannelEnum.ESB.getChannelType().equals(item.getCallChannelId())).findFirst();
                if (!channelCallCfgOptional.isPresent()) {
                    rglog.error("通道服务调用配置表无数据！,instId:<{}>,traceNo:<{}>,",
                            instId, manualAccoutingStatusQueryRequest.getHeader().getTraceNo());
                    throw new BizException(RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespCode(), RespCodeEnum.CONFIG_PARAM_NOT_EXIST.getRespDesc());
                }
                //获取通道服务调用配置
                ChannelCallCfg channelCallCfg = channelCallCfgOptional.get();
                manualAccoutingStatusQueryResponse = manualStatusQuery(instId, acctSeqNum, channelCallCfg);
            }
        } else if ("03".equals(acctOprType)) {
            /** 资金回退 */

            ManualReconErrTxnMapper manualReconErrTxnMapper = new ManualReconErrTxnMapperImpl();
            ReconErrTxn reconErrTxn = manualReconErrTxnMapper.selectReconErrTxnByprimaryKey(acctUnionKey);

            //查询商户订单，用户订单，支付订单
            PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
            List<PayOrderInfo> payOrderInfoList = payOrderInfoMapper.selectPayOrderInfoByOrigSysSeqNum(instId, reconErrTxn.getOrderId());
            List<PayOrderInfo> payOrderInfoListEsb = payOrderInfoList.stream().filter(item -> item.getTxnNum().equals("P01BM001")).sorted(Comparator.comparing(PayOrderInfo::getDstCallIndex)).collect(Collectors.toList());
            PayOrderInfo payOrderInfo = payOrderInfoListEsb.get(0);
            /**查询用户订单*/
            UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
            UserOrderInfo userOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(reconErrTxn.getInstId(), payOrderInfo.getMchntOrderId());
            /**查询商户订单*/
            MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
            List<MchntOrderInfo> mchntOrderInfoList = mchntOrderInfoMapper.selectMchntOrderInfoBySysSeqNum(reconErrTxn.getInstId(), payOrderInfo.getMchntOrderId());
            MchntOrderInfo mchntOrderInfo = mchntOrderInfoList.get(0);

            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
            DubboService dubboService = new DubboServiceImpl();
            TokenTransStateQueryRequest tranStateQueryRequest = new TokenTransStateQueryRequest();
            cc.rengu.igas.channel.upac.facade.base.Header header = new cc.rengu.igas.channel.upac.facade.base.Header();
            header.setTxnNum("0000000902");
            /**获取系统流水号*/
            header.setTraceNo(sysSeqNum);
            header.setChanlId(ChannelEnum.BOMP.getChannelType());
            header.setSrcSysId(AppParamConstant.SYS_ID);
            header.setBizType("00");
            header.setVersion("1.0.0");
            header.setTransTime(DateUtil.getCurrentTime());
            header.setTransDate(DateUtil.getCurrentDate());
            header.setInstId(instId);
            tranStateQueryRequest.setHeader(header);
            tranStateQueryRequest.setAccessType("0");
            tranStateQueryRequest.setChannelType("07");
            tranStateQueryRequest.setProdType("TK01");
            tranStateQueryRequest.setOrderId(payOrderInfo.getSysSeqNum());
            tranStateQueryRequest.setOrderTime(payOrderInfo.getOrigTxnDate());
            Object object = dubboService.callDubboService("upacTokenPayService", "tokenTransStateQuery", tranStateQueryRequest);
            cc.rengu.igas.channel.upac.facade.result.Result<TokenTransStateQueryRespone> responseResult = (cc.rengu.igas.channel.upac.facade.result.Result<TokenTransStateQueryRespone>) object;
            rglog.info("成功状态判断：<{}>", responseResult.getResult().getRespCode().equals(RespCodeEnum.TRANS_SUCCESS.getRespCode()));
            if (null == object || !responseResult.isSuccess() ||
                    "ES000098".equals(responseResult.getResult().getRespCode()) ||
                    "04".equals(responseResult.getResult().getRespCode()) ||
                    "06".equals(responseResult.getResult().getRespCode())) {
                rglog.error("调用dubbo服务upacTokenPayService失败");
                throw new BizException(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode(), OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            } else if (responseResult.getResult().getRespCode().equals(RespCodeEnum.TRANS_SUCCESS.getRespCode())) {
                //交易成功
                rglog.info("1成功状态：<{}>", responseResult.getResult().getRespCode());
                List<MchntOrderInfo> mchntOrderInfoLists = new ArrayList<>();
                mchntOrderInfo.setTransStatus("1");
                mchntOrderInfo.setBizStatus("00");
                mchntOrderInfoLists.add(mchntOrderInfo);
                userOrderInfo.setTransStatus("1");
                userOrderInfo.setBizStatus("00");

                payOrderInfo.setTransStatus("1");
                payOrderInfo.setBizStatus("00");
                OrderService orderService = new OrderServiceImpl();
                orderService.updateOrderStatus(userOrderInfo, mchntOrderInfoLists, payOrderInfo);

                //退款 ：原交易 uptk：  交易状态 2或1  业务  02  esb   不改
                UserOrderInfo userOrderInfoOrig = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(reconErrTxn.getInstId(), reconErrTxn.getOrderId());
                if (userOrderInfoOrig != null) {
                    List<PayOrderInfo> payOrderInfoOrigList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, userOrderInfoOrig.getSysSeqNum());
                    List<PayOrderInfo> payOrderInfoOrigListUptk = payOrderInfoOrigList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.UPTK.getChannelType())).sorted(Comparator.comparing(PayOrderInfo::getDstCallIndex)).collect(Collectors.toList());
                    List<MchntOrderInfo> mchntOrderInfoOrigList = mchntOrderInfoMapper.selectMchntOrderInfoBySysSeqNum(reconErrTxn.getInstId(), userOrderInfoOrig.getSysSeqNum());
                    userOrderInfoOrig.setBizStatus("02");
                    userOrderInfoOrig.setTransStatus("2");
                    PayOrderInfo payOrderInfoOrigUptk = payOrderInfoOrigListUptk.get(0);
                    payOrderInfoOrigUptk.setBizStatus("02");
                    payOrderInfoOrigUptk.setTransStatus("2");
                    mchntOrderInfoOrigList.get(0).setTransStatus("2");
                    mchntOrderInfoOrigList.get(0).setBizStatus("02");
                    orderService.updateOrderStatus(userOrderInfoOrig, mchntOrderInfoOrigList, payOrderInfoOrigUptk);
                }

            } else if (!responseResult.getResult().getRespCode().equals(RespCodeEnum.TRANS_SUCCESS.getRespCode())) {
                //失败
                rglog.info("2失败状态：<{}>", responseResult.getResult().getRespCode());
                mchntOrderInfo.setTransStatus("2");
                mchntOrderInfo.setBizStatus("02");

                userOrderInfo.setTransStatus("2");
                userOrderInfo.setBizStatus("02");

                payOrderInfo.setTransStatus("2");
                payOrderInfo.setBizStatus("02");
                OrderService orderService = new OrderServiceImpl();
                orderService.updateOrderStatus(userOrderInfo, mchntOrderInfoList, payOrderInfo);
            } else {

            }
            String justStatus = (RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(responseResult.getResult().getRespCode())) ? AdjustStatusEnum.SUCCESS.getJustStatus() : AdjustStatusEnum.FAIL.getJustStatus();

            updateManualReconErrTxn(null, justStatus, reconErrTxn);

            /**添加差错调账流水*/
            ManualErrAdjustTxnMapper manualErrAdjustTxnMapper = new ManualErrAdjustTxnMapperImpl();
            ErrAdjustTxn errAdjustTxn = manualErrAdjustTxnMapper.selectManualErrAdjustTxnByprimaryKeyNew(acctUnionKey);
            String acctStatus = (RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(responseResult.getResult().getRespCode())) ? AcctStatusEnum.SUCCESS.getAcctStatus() : AcctStatusEnum.FAIL.getAcctStatus();
            errAdjustTxn.setAcctStatus(acctStatus);
            errAdjustTxn.setRespCode(responseResult.getResult().getRespCode());
            errAdjustTxn.setRespDesc(responseResult.getResult().getRespDesc());
            manualErrAdjustTxnMapper.updateManualErrAdjustTxnByprimaryKey(errAdjustTxn);

            manualAccoutingStatusQueryResponse.setAcctDate(errAdjustTxn.getAcctDate());
            manualAccoutingStatusQueryResponse.setFailureReason(errAdjustTxn.getRespDesc());
            manualAccoutingStatusQueryResponse.setAcctSeqNum(errAdjustTxn.getAcctSeqNum());
            manualAccoutingStatusQueryResponse.setAcctStatus(acctStatus);

            manualAccoutingStatusQueryResponse.setRespCode(errAdjustTxn.getRespCode());
            manualAccoutingStatusQueryResponse.setRespDesc(errAdjustTxn.getRespDesc());
        } else {
            rglog.error("入参检查失败!");
            throw new BizException(RespCodeEnum.PARAM_ERROR.getRespCode(), RespCodeEnum.PARAM_ERROR.getRespDesc());
        }
        /* 设置返回参数 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(manualAccoutingStatusQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ManualAccoutingStatusQueryResponse manualAccoutingStatusQueryResponse = (ManualAccoutingStatusQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(manualAccoutingStatusQueryResponse);
    }

    /**
     * 虚拟账户调账状态查询
     *
     * @param instId      多法人标识
     * @param reconErrTxn 对账不平表
     * @param acctSeqNum  记账流水号
     * @throws Exception 异常
     */
    private ManualAccoutingStatusQueryResponse virtualAccountStatusQuery(String instId, ReconErrTxn reconErrTxn, String acctSeqNum, ChannelCallCfg channelCallCfg) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        ManualAccoutingStatusQueryResponse manualAccoutingStatusQueryResponse = new ManualAccoutingStatusQueryResponse();

        VirtAcctTransStatusQueryRequest virtAcctTransStatusQueryRequest = new VirtAcctTransStatusQueryRequest();
        cc.rengu.igas.acct.facade.base.Header header = new cc.rengu.igas.acct.facade.base.Header();
        header.setInstId(instId);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setTraceNo(sysSeqNum + channelCallCfg.getCallIndex());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setChanlId(BompAppParamConstant.BOMP_SYS_ID);
        header.setBizType(channelCallCfg.getBizType());
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        virtAcctTransStatusQueryRequest.setHeader(header);
        virtAcctTransStatusQueryRequest.setCustomerId(reconErrTxn.getMchntNo());
        virtAcctTransStatusQueryRequest.setAcctType("00");
        virtAcctTransStatusQueryRequest.setAcctSeqNum(acctSeqNum);
        DubboService dubboService = new DubboServiceImpl();
        Result<VirtAcctTransStatusQueryResponse> result = (Result<VirtAcctTransStatusQueryResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_VIRT_ACCT_SERVICE, "virtAcctTransStatusQuery", virtAcctTransStatusQueryRequest);
        VirtAcctTransStatusQueryResponse virtAcctTransStatusQueryResponse = result.getResult();
        if (!result.isSuccess() || !BompAppParamConstant.DUBBO_SUCCESS.equals(virtAcctTransStatusQueryResponse.getRespCode())) {
            rglog.error("手工调账状态查询(调用duboo)处理失败，instId:<{}>,acctSeqNum:<{}>", instId, acctSeqNum);
            throw new BizException(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode(), OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
        } else {
            manualAccoutingStatusQueryResponse.setRespCode(virtAcctTransStatusQueryResponse.getRespCode());
            manualAccoutingStatusQueryResponse.setRespDesc(virtAcctTransStatusQueryResponse.getRespDesc());
            manualAccoutingStatusQueryResponse.setAcctDate(virtAcctTransStatusQueryResponse.getAcctDate());
            manualAccoutingStatusQueryResponse.setAcctSeqNum(virtAcctTransStatusQueryResponse.getAcctSeqNum());
            manualAccoutingStatusQueryResponse.setAcctStatus(virtAcctTransStatusQueryResponse.getTransStatus());
            return manualAccoutingStatusQueryResponse;
        }
    }

    /**
     * 营销对账状态查询
     *
     * @param instId
     * @param errAdjustTxn 对账流水表
     * @throws Exception 异常
     */
    private ManualAccoutingStatusQueryResponse marketingStatusQuery(String instId, ErrAdjustTxn errAdjustTxn, ChannelCallCfg channelCallCfg) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        ManualAccoutingStatusQueryResponse manualAccoutingStatusQueryResponse = new ManualAccoutingStatusQueryResponse();

        QueryTxnRegulationResultRequest queryTxnRegulationResultRequest = new QueryTxnRegulationResultRequest();
        cc.rengu.igas.momp.facade.base.Header header = new cc.rengu.igas.momp.facade.base.Header();
        header.setInstId(instId);
        header.setTxnNum(channelCallCfg.getCallChannelTxn());
        header.setTraceNo(sysSeqNum + channelCallCfg.getCallIndex());
        header.setSrcSysId(AppParamConstant.SYS_ID);
        header.setChanlId(BompAppParamConstant.BOMP_SYS_ID);
        header.setBizType(channelCallCfg.getBizType());
        header.setTransTime(DateUtil.getCurrentTime());
        header.setTransDate(DateUtil.getCurrentDate());
        queryTxnRegulationResultRequest.setHeader(header);
        TxnRegulationStatusBean orderDetail = new TxnRegulationStatusBean();
        orderDetail.setOrigTraceNo(errAdjustTxn.getPrimaryKey());
        orderDetail.setOrigTxnDate(errAdjustTxn.getAcctDate());
        queryTxnRegulationResultRequest.setOrderDetail(orderDetail);
        DubboService dubboService = new DubboServiceImpl();
        Result<QueryTxnRegulationResultResponse> result = (Result<QueryTxnRegulationResultResponse>) dubboService.callDubboService("paymentService", "queryTxnRegulationStatus", queryTxnRegulationResultRequest);
        QueryTxnRegulationResultResponse queryTxnRegulationResultResponse = result.getResult();
        if (!result.isSuccess() || !BompAppParamConstant.DUBBO_SUCCESS.equals(queryTxnRegulationResultResponse.getRespCode())) {
            rglog.error("手工调账状态查询(调用duboo)处理失败，instId:<{}>,traceNo:<{}>", instId, errAdjustTxn.getAcctSeqNum());
            throw new BizException(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode(), OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
        } else {
            manualAccoutingStatusQueryResponse.setRespCode(queryTxnRegulationResultResponse.getRespCode());
            manualAccoutingStatusQueryResponse.setRespDesc(queryTxnRegulationResultResponse.getRespDesc());
            manualAccoutingStatusQueryResponse.setAcctDate(queryTxnRegulationResultResponse.getOrderDetail().getOrigTxnDate());
            manualAccoutingStatusQueryResponse.setAcctSeqNum(queryTxnRegulationResultResponse.getOrderDetail().getOrigTraceNo());
            manualAccoutingStatusQueryResponse.setAcctStatus(queryTxnRegulationResultResponse.getOrderDetail().getRegulationStatus());
            return manualAccoutingStatusQueryResponse;
        }
    }

    /**
     * 手工挂账状态查询
     *
     * @param instId
     * @param acctSeqNum 挂账流水号
     * @throws Exception 异常
     */
    private ManualAccoutingStatusQueryResponse manualStatusQuery(String instId, String acctSeqNum, ChannelCallCfg channelCallCfg) throws Exception {
        ManualAccoutingStatusQueryResponse manualAccoutingStatusQueryResponse = new ManualAccoutingStatusQueryResponse();

        //TODO 调用交易查证


        return manualAccoutingStatusQueryResponse;
    }

    /**
     * 更新手工调账
     *
     * @param acctSeqNum
     * @param acctStatus  状态
     * @param reconErrTxn 手工调账
     * @throws Exception 异常
     */
    private void updateManualReconErrTxn(String acctSeqNum, String acctStatus, ReconErrTxn reconErrTxn) throws Exception {
        reconErrTxn.setAdjustStatus(acctStatus);
        ManualReconErrTxnMapper manualReconErrTxnMapper = new ManualReconErrTxnMapperImpl();
        int iReturnCode = manualReconErrTxnMapper.updateReconErrTxnByprimaryKey(reconErrTxn);
        if (0 != iReturnCode) {
            throw new BizException(RespCodeEnum.DATABASE_OPR_ERROR.getRespCode(), RespCodeEnum.DATABASE_OPR_ERROR.getRespDesc());
        }
    }


}
